package com.huatai.datacenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.datacenter.config.CustomDataModelException;
import com.huatai.datacenter.entity.CodeEntity;
import com.huatai.datacenter.entity.datamodel.DataModelApplyEntity;
import com.huatai.datacenter.mapper.DataModelApplyMapper;
import com.huatai.datacenter.service.CodeService;
import com.huatai.datacenter.service.DataModelApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;


/**
 * @author gusiyi
 * @version 1.0
 * @className DataModelApplyServiceImpl
 * @description 描述：
 * @date 2023/5/26 16:49
 */
@Service
public class DataModelApplyServiceImpl extends ServiceImpl<DataModelApplyMapper,DataModelApplyEntity> implements DataModelApplyService {

	@Autowired
	private CodeService codeService;

	@Override
	public Page<DataModelApplyEntity> getDataModelApplyList(DataModelApplyEntity dataModelApplyEntity) {
		Page<DataModelApplyEntity> dataModelApplyEntityPage = new Page<>(dataModelApplyEntity.getPageIndex(),dataModelApplyEntity.getPageSize());
		LambdaQueryWrapper<DataModelApplyEntity> applyQuery = Wrappers.lambdaQuery();
		applyQuery
			.eq(DataModelApplyEntity::getDeleted,false)
			.like(StringUtil.isNotBlank(dataModelApplyEntity.getDataModelName()),DataModelApplyEntity::getDataModelName,dataModelApplyEntity.getDataModelName())
			.eq(ObjectUtil.isNotEmpty(dataModelApplyEntity.getApplyTypeNum()),DataModelApplyEntity::getApplyTypeNum,dataModelApplyEntity.getApplyTypeNum())
			.orderByDesc(DataModelApplyEntity::getUpdateTime);
		return page(dataModelApplyEntityPage,applyQuery);
	}


	@Override
	public boolean saveDataModelApplyInstance(Long dataModelId, String dataModelName,Long applyId, String applyName, String applyTypeName) {
		//该接口由后台使用，前台不使用所以未使用@Valid来进行参数校验
		Assert.notNull(dataModelId,"dataModelId不能为空");
		Assert.notNull(dataModelName,"dataModelName不能为空");
		Assert.notNull(applyId,"applyId不能为空");
		Assert.notNull(applyName,"applyName不能为空");
		Assert.notNull(applyTypeName,"applyTypeName不能为空");
		LambdaQueryWrapper<CodeEntity> dataModelApplyTypeQuery = Wrappers.lambdaQuery();
		dataModelApplyTypeQuery
			.eq(CodeEntity::getType,"dataModelApplyType")
			.eq(CodeEntity::getDeleted,false)
			.orderByAsc(CodeEntity::getSorted);
		List<CodeEntity> codeEntities = codeService.list(dataModelApplyTypeQuery);
		DataModelApplyEntity dataModelApplyEntity = new DataModelApplyEntity();
		dataModelApplyEntity.setDataModelId(dataModelId);
		dataModelApplyEntity.setDataModelName(dataModelName);
		dataModelApplyEntity.setApplyId(applyId);
		dataModelApplyEntity.setApplyName(applyName);
		boolean codeFlag = false;
		for (CodeEntity codeEntity : codeEntities) {
			if (codeEntity.getName().equals(applyTypeName)){
				dataModelApplyEntity.setApplyTypeNum(codeEntity.getNum());
				dataModelApplyEntity.setApplyTypeName(applyTypeName);
				codeFlag = true;
				break;
			}
		}
		if (codeFlag){
			return save(dataModelApplyEntity);
		} else {
			throw new CustomDataModelException("请检查applyTypeName参数是否正确，并在tb_code表中确认是否有对应的类型");
		}
	}

	@Override
	public boolean deleteApplyContentByApplyId(List<Long> deleteApplyIds){
		LambdaQueryWrapper<DataModelApplyEntity> deleteApplyQuery = Wrappers.lambdaQuery();
		deleteApplyQuery.in(DataModelApplyEntity::getApplyId,deleteApplyIds);
		DataModelApplyEntity deleteModel = new DataModelApplyEntity();
		deleteModel.setDeleted(true);
		return update(deleteModel,deleteApplyQuery);
	}
}
